package util;

import java.awt.Component;
import java.io.File;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


/**
 * Holds useful utility methods
 * @author TKington
 *
 */
public class Util {
	/** Exception handler for AWT thread. */
	public static ExceptionHandler handler = new ExceptionHandler();
	
	/**
	 * Loads an icon from the images directory.
	 * @param imageName the name of the icon
	 * @return the new ImageIcon object
	 */
	public static ImageIcon getIcon(String imageName) {
	    String imagePath = "images/" + imageName; //$NON-NLS-1$
	    ClassLoader cl = Util.class.getClassLoader();
	    URL url = cl.getResource(imagePath);
	    return new ImageIcon(url);
	}
	
	/** The JFileChooser. */
	private static JFileChooser chooser;
    
    static {
        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setAcceptAllFileFilterUsed(false);
    }

    /**
     * Presents a JFileChooser dialog and allows the user to select a file.
     * @param owner the component that will be the parent of the JFileChooser
     * @param filter a SimpleFileFilter used to filter visible files
     * @param title the dialog title
     * @param save true if saving, false if opening - controls which type of dialog
     * 			is shown
     * @param buttonText the text to display on select button
     * @return the path of the selected file
     */
	public static String chooseFile(Component owner, SimpleFileFilter filter, String title,
	                                boolean save, String buttonText) {
	    chooser.setDialogTitle(title);
	    chooser.resetChoosableFileFilters();
	    chooser.setFileFilter(filter);
	    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	    chooser.setApproveButtonText(buttonText);
	    
	    int ret;
	    if(save)
	        ret = chooser.showSaveDialog(owner);
	    else ret = chooser.showOpenDialog(owner);
	    
	    if(ret != JFileChooser.APPROVE_OPTION)
	        return null;
	    
	    File f = chooser.getSelectedFile();
	    if(save) {
	        if(f.exists()) {
	            int choice = JOptionPane.showConfirmDialog(owner,
	                                          f.getName() + " " + "already exists. Overwrite?",
	                                          null, JOptionPane.YES_NO_OPTION);
	            if(choice == JOptionPane.NO_OPTION)
	                return null;
	        }
	    }
	    else if(!f.exists()) {
	        JOptionPane.showMessageDialog(owner, f.getName() + " " + "does not exist");
	        return null;
	    }
	    
	    if(!f.getName().toLowerCase().endsWith(filter.getExt()))
	        return f.getPath() + "." + filter.getExt();
	    return f.getPath();
	}

	/**
	 * Displays a message dialog, and logs the Throwable to viewer.log.
	 * @param t the Throwable
	 */
	public static void log(Throwable t) { handler.handle(t); }

	/**
	 * Logs the Throwable to viewer.log.
	 * @param t the Throwable
	 */
	public static void logSilent(Throwable t) { handler.logSilent(t); }
}
